package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.jackrain.clouddata.oms.util.KeyValueUtils.keyAppend;
import static com.jackrain.clouddata.oms.util.KeyValueUtils.valueAppend;

/**
 * Created by nathan on 2017/8/15.
 */
@Component
public class SaleDao {

    @Transactional
    public void execute(JSONObject mainJson, JSONArray itemArray,Long clientId){

        String countSql = "SELECT COUNT(*) FROM M_SALE WHERE AD_CLIENT_ID = ? AND BILLNO = ?";
        int count = QueryEngine.getInstance().doQueryInt(countSql,new Object[]{clientId,mainJson.getString("BILLNO")});

        if (count==0){
            for (int i =0;i<itemArray.size();i++){
                JSONObject itemJson = itemArray.getJSONObject(i);
                //获取字表名称
                String itemName = itemJson.getString("name")        ;
                //获取value
                JSONArray valueArray = itemJson.getJSONArray("value");
                if (valueArray != null && valueArray.size()>0){
                    JSONArray valuesA = new JSONArray();
                    for (int j=0;j<valueArray.size();j++){
                        JSONObject valueJson = JSON.parseObject(JSON.toJSONString(valueArray.getJSONObject(j),new SerializerFeature[]{SerializerFeature.WriteMapNullValue}), Feature.OrderedField);
                        for (int o=0;o<valueJson.keySet().size();o++){
                            String key = (String) valueJson.keySet().toArray()[o];
                            if (key.contains("DATE")){
                                valueJson.put(key,TypeUtils.castToDate(valueArray.getJSONObject(j).getString(key)));
                            }
                        }
                        valueJson.put("AD_CLIENT_ID",clientId);
                        valueJson.put("ID", Tools.getSequence(itemName));
                        valuesA.set(j,valueJson);
                    }
                    StringBuffer saleitemInsertSql = new StringBuffer();
                    saleitemInsertSql.append("INSERT INTO "+itemName+"("+keyAppend(valuesA.getJSONObject(0).keySet())+") VALUES(").append(valueAppend(valuesA.getJSONObject(0).size())).append(")");

                    QueryEngine.getInstance().executeBatchUpdate(saleitemInsertSql.toString(),valuesA);

                }
            }

            JSONObject saleJson = JSON.parseObject(JSON.toJSONString(mainJson,new SerializerFeature[]{SerializerFeature.WriteMapNullValue}), Feature.OrderedField);
            for (int i=0;i<saleJson.keySet().size();i++){
                String key = (String) saleJson.keySet().toArray()[i];
                String value = saleJson.getString(key);
                if(value == null){
                    saleJson.remove(key);
                }else if (value != null && key.contains("DATE")){
                    saleJson.put(key,TypeUtils.castToDate(mainJson.getString(key)));
                }
            }
            saleJson.put("AD_CLIENT_ID",clientId);

            saleJson.put("ID", Tools.getSequence("M_SALE"));
            StringBuffer saleInsertSq = new StringBuffer();
            saleInsertSq.append("INSERT INTO M_SALE("+keyAppend(saleJson.keySet())+") VALUES(").append(valueAppend(saleJson.size())).append(")");
            QueryEngine.getInstance().executeUpdate(saleInsertSq.toString(),saleJson.values().toArray());
        }

    }

}
